<?php
apf_require_class('APF_Controller');
apf_require_class('Solr_Bll_AppBiz');
class Appconfig_IndexController extends APF_Controller {

    public static function build_uri() {
        $base_domain = APF::get_instance()->get_config("base_domain");
        $schema = APF::get_instance()->get_request()->is_secure() ? "https" : "http";
        $url = "{$schema}://{$base_domain}/appconfig/";
        return $url;
    }

    public function handle_request() {

        $request = APF::get_instance()->get_request();
        $params = $request->get_parameters();

        $app_biz=Solr_Bll_AppBiz::get_instance();

        $action=$params['action'];
        if($action=='add_hostname'&&$params['hostname']){
            $app_biz->insert_hostname($params['hostname']);
            APF::get_instance()->get_response()->redirect(Appconfig_IndexController::build_uri());
        }elseif($action=='add_webapp'&&$params['webapp']&&$params['solrname']){
            $app_biz->insert_webapp(trim($params['webapp']),trim($params['solrname']),trim($params['port']));
            APF::get_instance()->get_response()->redirect(Appconfig_IndexController::build_uri());
        }

        $hostnames=$app_biz->get_all_hostname();
        $webapps=$app_biz->get_all_webapp();

        $request->set_attribute('hostnames',$hostnames);
        $request->set_attribute('webapps',$webapps);

        return 'Appconfig_Index';
    }
}

